package com.demo.springbootdemo.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("redis")
@RequiredArgsConstructor
public class RedisController {
    private final RedisTemplate redisTemplate;

    @GetMapping("setString")
    public String setString(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
        return key + "=" + value;
    }

    @GetMapping("getString")
    public String getString(String key) {
        return key + "=" + redisTemplate.opsForValue().get(key);
    }

    @GetMapping("setObject")
    public String setObject(String key, String name, String value) {
        HashMap<Object, Object> map = new HashMap<>();
        map.put(name, value);
        redisTemplate.opsForHash().putAll(key, map);
        return key + "=" + value;
    }

    @GetMapping("getObject")
    public Map getObject(String key) {
        return redisTemplate.opsForHash().entries(key);
    }
}
